package com.acme.bankapp.listeners.email;

import java.util.ArrayList;

public class Queue {
	private ArrayList<Email> emaillist = new ArrayList<Email>();
	private boolean flag=true; 
	void add(Email email) {
		emaillist.add(email);
		synchronized (emaillist) {
			emaillist.notify();
		}
	}

	public Email get() {
		synchronized (emaillist) {
			try {
				while (emaillist.isEmpty()) {
					emaillist.wait();
					if (!flag){
						return null;
					}
				}
			} catch (InterruptedException e) {

			}
			return emaillist.remove(0);
		}
	}

	public void close() {
		synchronized (emaillist) {
			emaillist.notifyAll();
			flag=false;
		}
	}

}
